//
//  VideoDetailViewController.m
//  IFengNews
//
//  Created by lanou3g on 16/3/9.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "VideoDetailViewController.h"
#import "UrlRequestManager.h"
#import "VideoListModel.h"
#import "VideoTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "VideoPlayerViewController.h"
@interface VideoDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *videoAllData;
@property (weak, nonatomic) IBOutlet UITableView *videoTableView;
@end

@implementation VideoDetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self requestData];
}

- (void)initView{
    //两个tableView设置代理
    self.videoTableView.delegate = self;
    self.videoTableView.dataSource = self;
    //tableViewCell的注册
    [self.videoTableView registerNib:[UINib nibWithNibName:@"VideoTableViewCell" bundle:nil] forCellReuseIdentifier:@"VideoTableViewCell"];
}
- (void)requestData{
    //视频的数据
    [UrlRequestManager requestWith:GET URL:self.url condition:nil SuccessBlock:^(id item) {
        NSArray *array = item[self.category];
        for (NSDictionary *dic in array) {
            VideoListModel *model = [[VideoListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.videoAllData addObject:model];
        }
        //主线程中刷新数据
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.videoTableView reloadData];
        });
    } failBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark - 视频videoTableView的代理方法
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.videoAllData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 230;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    VideoListModel *model = self.videoAllData[indexPath.row];
    cell.title.text = model.title;
    cell.descrip.text = model.descrip;
    [cell.imV sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - 40)/2.0, 54, 30, 24)];
    imageV.image = [UIImage imageNamed:@"doc_video_bottom_icon"];
    [cell.imV addSubview:imageV];
                     
    cell.length.text = [self getTimeStrFromTime:model.length];
    cell.playCount.text = [self getPlayCountFromPlayCount:model.playCount];
    cell.replyCount.text = [NSString stringWithFormat:@"%ld",model.replyCount];
    cell.videosource.text = model.videosource;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //视屏videoTableView的代理方法
    VideoPlayerViewController *videoPlayerVC = [[VideoPlayerViewController alloc]init];
    VideoListModel *model = self.videoAllData[indexPath.row];
    videoPlayerVC.videoUrl = model.mp4_url;
    videoPlayerVC.title = model.title;
    [self.navigationController pushViewController:videoPlayerVC animated:YES];
}
#pragma mark -私有方法
//返回播放数量
- (NSString *)getPlayCountFromPlayCount:(NSInteger )playCount{
    if (playCount >= 10000) {
        return [NSString stringWithFormat:@"%.1f万",playCount/10000.0];
    }else{
        return [NSString stringWithFormat:@"%ld",playCount];
    }
}
//返回处理过的时间字符串
- (NSString *)getTimeStrFromTime:(NSInteger)time{
    NSInteger second = time % 60;
    NSInteger minute = time / 60;
    return [NSString stringWithFormat:@"%02ld:%02ld",minute,second];
}
#pragma mark - 懒加载
- (NSMutableArray *)videoAllData{
    if (!_videoAllData) {
        _videoAllData = [NSMutableArray array];
    }
    return _videoAllData;
}
@end
